package com.example.send;

import com.example.send.service.PanBaiduService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.FileWriter;
import java.io.IOException;

@SpringBootApplication
public class SendApplication {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(SendApplication.class, args);

        new Thread(() ->{
            PanBaiduService panBaiduService = context.getBean(PanBaiduService.class);
            FileWriter fw = null;
            try {
                fw = new FileWriter("D:/txt/files0119.txt");
                while (true) {
                    String text = null;
                    try {
                        text = panBaiduService.block.take();
                        System.out.println(text);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    fw.write(text);
                    fw.flush();
                    if ("<end>".equals(text)) {
                        break;
                    }
                }
                fw.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fw != null) {
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        PanBaiduService panService = context.getBean(PanBaiduService.class);
        panService.start();
    }

}
